<template>
  <div>
    <el-dialog width="500px" title="修改密码" :visible="true" :before-close="cancelBtn">
      <el-form>
        <el-form-item label="旧密码">
          <el-input type="text" v-model="original" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input type="password" v-model="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input type="password" v-model="password_confirmation" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="confirmPass">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {editPassword} from "@/api"
import {removeToken} from "@/utils/auth";

export default {
  name: "editPassword",
  props:['isShow','closeAlert'],
  data(){
    return {
      original:'',
      password:'',
      password_confirmation	:''
    }
  },
  mounted() {
  },
  methods:{
    // 关闭弹窗
    cancelBtn(){
      this.$emit('closeAlert',false)
    },
    async confirmPass(){
      if(this.password.trim() != '' && this.password_confirmation.trim() != ''){
        if(this.password == this.password_confirmation){
          await editPassword({
            original:this.original,
            password:this.password,
            password_confirmation	:this.password_confirmation
          }).then(res=>{
            // console.log(res)
            if(res.code==10000){
              this.$message({
                message: '修改成功,请重新登陆',
                type: 'success'
              });
              removeToken()
              this.$router.push(`/`)
            }
          })
        }else{
          this.$message({
            message: '两次密码不一致,请重新输入',
            type: 'error'
          });
        }
      }

    }
  }
}
</script>

<style scoped>

</style>
